package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.servicce.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 482944566@qq.com
 * @version 1.0
 * @date 2024-12-08
 */

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private AuditService auditService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务：applicationSaveParam={}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增申请单，初始状态为‘已发起'
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.insert(application);

        //调用审批业务层，为当前申请单同步新增对应的审批数据
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务：applicationQuery={}",applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for (int i=0;i<list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancelApplication(Long id) {
        log.debug("撤销申请单业务:id={}",id);
        Application application = new Application();
        application.setId(id);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.update(application);

        auditService.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆：申请单编号="+applicationId+"车辆编号="+vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("归还车辆业务：applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(null);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.back(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }

    /*
     * 本方法用于给当前传入的申请单VO的审批人数据赋值(auditUserIdList与auditUsernameList)
     */
    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,分别用来保存申请单对应的审批人姓名与id集合
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //2.根据传入的申请单对象id查出对应的审批数据,注意:审批数据可能有多条
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(
                applicationVO.getId());
        //3.依次遍历每一个审批单对象
        for(int i=0; i<auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.取出审批VO中的审批人id,装入上方的空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据刚刚取出的审批人id,查出此人姓名,装入上方的空集合中(之前在用户mapper层写过此方法)
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个拼接工具,帮我们将所有审批人姓名连成一个字符串
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //将拼接好的审批人姓名字符串赋值给申请单VO的AuditUsernameList属性
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        //将准备好的审批人id集合赋值给申请单VO的AuditUserIdList属性
        applicationVO.setAuditUserIdList(auditUserIdList);

    }
}
